package com.linsen.erp.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linsen.erp.admin.entity.ErrorLogEntity;
import com.linsen.erp.admin.mapper.ErrorLogMapper;
import com.linsen.erp.admin.provider.qo.ErrorLogQo;
import com.linsen.erp.admin.service.IErrorLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 错误日志 服务实现类
 * </p>
 *
 * @author zhaoyong
 * @since 2023-07-24
 */
@Service
@RequiredArgsConstructor
public class ErrorLogServiceImpl extends ServiceImpl<ErrorLogMapper, ErrorLogEntity> implements IErrorLogService {

    /** 封装分页列表查询的wrapper对象 */
    private Wrapper<ErrorLogEntity> getWrapper(ErrorLogQo errorLogQo){
        String url = errorLogQo.getUrl();
        String requestParam = errorLogQo.getRequestParam();
        String errorInfo = errorLogQo.getErrorInfo();
        String ip = errorLogQo.getIp();
        String createByName = errorLogQo.getCreateByName();
        LocalDateTime startTime = errorLogQo.getStartTime();
        LocalDateTime endTime = errorLogQo.getEndTime();

        return new LambdaQueryWrapper<ErrorLogEntity>()
                .like(ObjectUtil.isNotEmpty(url),ErrorLogEntity::getUrl,url)
                .like(ObjectUtil.isNotEmpty(requestParam),ErrorLogEntity::getRequestParam,requestParam)
                .like(ObjectUtil.isNotEmpty(errorInfo),ErrorLogEntity::getErrorInfo,errorInfo)
                .like(ObjectUtil.isNotEmpty(ip),ErrorLogEntity::getIp,ip)
                .like(ObjectUtil.isNotEmpty(createByName),ErrorLogEntity::getCreateByName,createByName)
                .ge(ObjectUtil.isNotNull(startTime),ErrorLogEntity::getCreateTime, startTime)
                .le(ObjectUtil.isNotNull(endTime),ErrorLogEntity::getCreateTime, endTime)
                .orderByDesc(ErrorLogEntity::getCreateTime);
    }

    @Override
    public Page<ErrorLogEntity> page(ErrorLogQo qo) {
        return this.page(new Page<>(qo.getCurrent(), qo.getSize()), getWrapper(qo));
    }
}
